/*
 * @Author: NtyAaron
 * @Date: 2021-03-23 14:46:41
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-07-02 10:16:01
 * @Description: 地图
 * @FilePath: \dwq\pages\interactionMap\interactionMap.js
 */
// const plugin = requirePlugin('routePlan')
import { _t } from '../../i18n/base'
const chooseLocation = requirePlugin('chooseLocation')

const key = 'V6NBZ-DIMK3-I553O-YYUNH-SRG6J-S4FTK'
const referer = 'GoGBA'
import { getPlace } from '../../api/pubService/pubService'
var timer = 0
Page({
  data: {
    latitude: 23.099994,
    longitude: 116.32452,
    value: '',
    poiFlag: false,
    addrList: [],
  },
  // 定位地图
  async locateCurrentLocation(e) {
    let res = await wx.getLocation({
      type: 'wgs84',
      altitude: false,
    })
    this.setData({
      latitude: res.latitude,
      longitude: res.longitude,
    })
    if (e) {
      let { result } = await getPlace({
        keyword: e || '',
        lat: res.latitude,
        lng: res.longitude,
        page_index: 1,
        page_size: 10,
      })
      this.setData({
        addrList: result,
      })
    }
  },
  // 搜索防抖
  debounce(fn, delay) {
    clearTimeout(timer)
    timer = setTimeout(() => {
      fn()
    }, delay)
  },
  // 搜索
  onSearch({ detail }) {
    this.locateCurrentLocation(detail)
  },
  // 清空删除
  clearSearch() {
    this.setData({
      addrList: [],
    })
  },
  // 监听输入
  onChange({ detail }) {
    if (detail.length == 0) {
      this.clearSearch()
    } else {
      this.debounce(() => {
        if (detail.length > 1) {
          this.locateCurrentLocation(detail)
        }
      }, 1000)
    }
  },
  // 点击坐标
  clickAddress(e) {
    let result = e.target.dataset.item
    let endPoint = JSON.stringify({
      name: result.title,
      latitude: result.location.lat,
      longitude: result.location.lng,
    })
    wx.navigateTo({
      url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint + '&navigation=1',
    })
  },
  // 跳转交通页面
  async toTrafficHandle() {
    try {
      const category = ''
      if (this.data.locaInfo) {
        const location = JSON.stringify({
          latitude: this.data.locaInfo.latitude,
          longitude: this.data.locaInfo.longitude,
        })
        await wx.navigateTo({
          url: 'plugin://chooseLocation/index?key=' + key + '&referer=' + referer + '&location=' + location + '&category=' + category,
        })
      } else {
        let res = await wx.getLocation({
          type: 'wgs84',
          altitude: false,
        })
        const location = JSON.stringify({
          latitude: res.latitude,
          longitude: res.longitude,
        })
        await wx.navigateTo({
          url: 'plugin://chooseLocation/index?key=' + key + '&referer=' + referer + '&location=' + location + '&category=' + category,
        })
      }
    } catch (error) {
      console.log(error)
    }
  },
  // 跳转内置地图调起第三方app
  async toMapHandle() {
    try {
      if (this.data.locaInfo) {
        const latitude = this.data.locaInfo.latitude
        const longitude = this.data.locaInfo.longitude
        let data = await wx.openLocation({
          latitude,
          longitude,
          scale: 18,
        })
        console.log(data)
      } else {
        let res = await wx.getLocation({
          type: 'gcj02',
        })
        const latitude = res.latitude
        const longitude = res.longitude
        let data = await wx.openLocation({
          latitude,
          longitude,
          scale: 18,
        })
        console.log(data)
      }
    } catch (error) {
      console.log(error)
    }
  },
  // POI点击确认选点后
  poiConfirmHandle() {
    const result = chooseLocation.getLocation()
    if (result) {
      let endPoint = JSON.stringify({
        name: result.name,
        latitude: result.latitude,
        longitude: result.longitude,
      })
      wx.navigateTo({
        url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint + '&navigation=1',
      })
    }
  },
  // 第一次确认poi之后，如果用户返回，应该干掉这个标志
  autoClearPoi() {
    chooseLocation.setLocation(null)
  },
  onShow() {
    this.poiConfirmHandle()
    this.autoClearPoi()
  },
  onUnload() {
    this.autoClearPoi()
  },
  // 加载一次
  onLoad(e) {
    this.setData({
      lang: _t(),
    })
    wx.setNavigationBarTitle({
      title: this.data.lang['互动地图'],
    })
    if (e.location) {
      let locaName = e.location
      this.locateCurrentLocation(locaName)
    }
    if (e.locaInfo) {
      this.setData({
        locaInfo: JSON.parse(e.locaInfo),
      })
    }
    this.locateCurrentLocation()
  },
})
